package util;

import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author yujie
 * @createTime 2023/1/3 18:27
 * @description
 */
@Slf4j
public class Util {

    public static File createTempPdfFile(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = dateFormat.format(new Date())+".pdf";
        String dir = Prop.get("documentPOJIE.output.path");
        File file = new File(dir + "/" + dateFormat.format(new Date()) + "/" + fileName);

        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            log.error("文件创建失败");
            throw new RuntimeException(e);
        }
        return file;
    }

    public static File createTempWordFile(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = dateFormat.format(new Date())+".docx";
        String dir = Prop.get("documentPOJIE.output.path");
        File file = new File(dir + "/"+ dateFormat.format(new Date()) + "/" + fileName);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            log.error("文件创建失败");
            throw new RuntimeException(e);
        }
        return file;
    }

    public static File createmodouyuword(String name, String cdir){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = name +".docx";
        String dir = Prop.get("modouyu.output.path");
        File file = new File(dir + "/" + cdir +"/" + fileName);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            log.error("文件创建失败");
            throw new RuntimeException(e);
        }
        return file;
    }

    public static File createmodouyuword(String name){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = name +".docx";
        String dir = Prop.get("modouyu.output.path");
        File file = new File(dir + "/" + UUID.randomUUID() +"/" + fileName);
        if(!file.exists()){
            file.getParentFile().mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            log.error("文件创建失败");
            throw new RuntimeException(e);
        }
        return file;
    }

    public static File wordTextReplace(Map<String, Object> map, String name, String cdir) {

        try {
            File tempWordFile = Util.createmodouyuword(name, cdir );
            Document document = new Document(Util.class.getClassLoader().getResourceAsStream("modouyu.docx"));
            document = SignContractUtil.fillData(document, map);
            SignContractUtil.saveDocument(document, tempWordFile, SaveFormat.DOCX);
            return tempWordFile;
        } catch (Exception e) {
            log.info("文件名称："+name+"创建失败");
            return null;
        }
    }

    public static File wordTextReplace(Map<String, Object> map, String name) {

        try {
            File tempWordFile = Util.createmodouyuword(name);
            Document document = new Document(Util.class.getClassLoader().getResourceAsStream("modouyu.docx"));
            document = SignContractUtil.fillData(document, map);
            SignContractUtil.saveDocument(document, tempWordFile, SaveFormat.DOCX);
            return tempWordFile;
        } catch (Exception e) {
            log.info("文件名称创建失败");
            return null;
        }
    }

}
